package com.mrx.commons.redis;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Accessors;

import java.util.concurrent.TimeUnit;

@Data
@Accessors(chain = true)
@RequiredArgsConstructor(staticName = "of")
public class ValueWrapper {

    /**
     * 被包装的对象
     */
    private final Object obj;

    /**
     * 该对象存活时间
     */
    private long timeout = 30;

    /**
     * 存活时间单位
     */
    private TimeUnit unit = TimeUnit.DAYS;

    /**
     * 创建对象的时间
     */
    private long createTime = System.currentTimeMillis();

    public ValueWrapper setTimeout(long timeout) {
        this.timeout = timeout;
        // 重新初始化 createTime
        this.createTime = System.currentTimeMillis();
        return this;
    }

    public boolean isExpired() {
        return createTime + unit.toMillis(timeout) <= System.currentTimeMillis();
    }

}
